package com.windfone.web.action;

import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.Constant;
import com.windfone.common.PageUtil;
import com.windfone.model.News;
import com.windfone.model.NewsType;
import com.windfone.service.NewsService;

public class NewsAction extends ActionSupport {
	private NewsService newsService;
	private NewsType newsTypeBean;
	private News newsBean;
	private int beginRecord;
	private int currentPage;
	private int nextPage;
	private int totalPage;
	private int prevPage;
	private List<NewsType> newsTypeList;
	private List<News> newsList;
	private List<News> pageewsList;
	private int newsId;
	private int newsTypeId;
	
	private String typeName;
	private String description;
	
	private int userId;
	private Date createTime;
	private String content;
	private String title;
	private String summary;

	public void setNewsId(int newsId) {
		this.newsId = newsId;
	}
	public int getUserId() {
		return userId;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public String getContent() {
		return content;
	}
	public List<News> getPageewsList() {
		return pageewsList;
	}
	public void setPageewsList(List<News> pageewsList) {
		this.pageewsList = pageewsList;
	}
	public String getTitle() {
		return title;
	}
	public String getSummary() {
		return summary;
	}
 
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public void setSummary(String summary) {
		this.summary = summary;
	}
 
	
	public List<News> getNewsList() {
		return newsList;
	}
	
	public int getNewsId() {
		return newsId;
	}
	
	public void setNewsList(List<News> newsList) {
		this.newsList = newsList;
	}
	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public String getTypeName() {
		return typeName;
	}

	public String getDescription() {
		return description;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public NewsType getNewsTypeBean() {
		return newsTypeBean;
	}

	public News getNewsBean() {
		return newsBean;
	}

	public void setNewsTypeBean(NewsType newsTypeBean) {
		this.newsTypeBean = newsTypeBean;
	}

	public void setNewsBean(News newsBean) {
		this.newsBean = newsBean;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getNewsTypeId() {
		return newsTypeId;
	}

	public void setNewsTypeId(int newsTypeId) {
		this.newsTypeId = newsTypeId;
	}

	public List<NewsType> getNewsTypeList() {
		return newsTypeList;
	}

	public void setNewsTypeList(List<NewsType> newsTypeList) {
		this.newsTypeList = newsTypeList;
	}

	public NewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}
	
	public String newsType() throws Exception {
		return this.SUCCESS;
	}
	
	public String addNewsType() throws Exception {
		NewsType bean = new NewsType();
		bean.setDescription(description);
		bean.setTypeName(typeName);
		newsService.insertNewsType(bean);
		return this.SUCCESS;
	}
	public String updateNewsType() throws Exception {
		NewsType bean = new NewsType();
		bean.setDescription(description);
		bean.setTypeName(typeName);
		bean.setId(newsTypeId);
		newsService.updateNewsType(bean);
		return this.SUCCESS;
	}
	
	public String deleteNewsType() throws Exception {
		newsService.deleteNewsType(newsTypeId);
		return this.SUCCESS;
	}
	public String beforUpdateNewsType() throws Exception {
		newsTypeBean = newsService.getNewsTypeById(newsTypeId).get(0);
		return this.SUCCESS;
	}
	public String newsTypeList() throws Exception {
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		int totalRecords = newsService.getAllNewsTypeRecorderNumber() ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		newsTypeList = newsService.getAllNewsType();
		return SUCCESS;
	}
	
	
	public String news() throws Exception {
		newsTypeList = newsService.getAllNewsType();
		return this.SUCCESS;
	}
	public String addNews() throws Exception {
		News bean = new News();
		bean.setContent(content);
		bean.setTitle(title);
		bean.setNewsTypeId(newsTypeId);
		bean.setSummary(summary);
		bean.setUserId(userId);
		newsService.insertNews(bean);
		return this.SUCCESS;
	}
	public String deleteNews() throws Exception {
		newsService.deleteNews(newsId);
		return this.SUCCESS;
	}
	public String newsList() throws Exception {
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		int totalRecords = newsService.getAllRecorderNumber() ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		newsList = newsService.getAllNews( beginRecord );
		return this.SUCCESS;
	}
	public String beforUpdateNews() throws Exception {
		newsBean = newsService.getNewsById(newsId).get(0);
		newsTypeList = newsService.getAllNewsType();
		return this.SUCCESS;
	}
	public String updateNews() throws Exception {
		News bean = new News();
		bean.setContent(content);
		bean.setNewsTypeId(newsTypeId);
		bean.setSummary(summary);
		bean.setTitle(title);
		bean.setUserId(userId);
		bean.setId(newsId);
		newsService.updateNews(bean);
		return this.SUCCESS;
	}
	
	public String newsDetail() throws Exception {
		newsBean =newsService.getNewsById(newsId).get(0);
		return this.SUCCESS;
	}
	
	public String newsCenter() throws Exception {
		newsTypeList = newsService.getAllNewsType();
		if ( newsTypeId ==0 || newsTypeId ==-1)
			newsTypeId =newsTypeList.get(0).getId();
		pageewsList = newsService.getNewsByNewtypeId(newsTypeId);
		return this.SUCCESS;
	}
	
	
}
